package com.sba;

import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.sba.core.web.interceptor.LoginInterceptor;
import com.sba.core.web.interceptor.ThemeInterceptor;

/**
 *
 * @author feiliming
 *
 */
@SpringBootConfiguration
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

	@Value("${ssh.admin-path}")
	protected String adminPath;

	@Value("${ssh.business-path}")
	protected String businessPath;

	//@Value("${ssh.upload-image-dir}")
	//protected String uploadPath;

	@Value("${ssh.wechat-path}")
	protected String wechatPath;

	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setMaxFileSize("1024KB");
		factory.setMaxRequestSize("1024KB");
		//factory.setLocation("/webapps/upload/image");
		return factory.createMultipartConfig();
	}

	@Bean
	public ThemeInterceptor themeInterceptor(){
		ThemeInterceptor ti = new ThemeInterceptor();
		return ti;
	}
	@Bean
	public LoginInterceptor loginInterceptor(){
		LoginInterceptor li = new LoginInterceptor();
		return li;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {


		super.addInterceptors(registry);//暂时先放这，稍后会改到session
        registry.addInterceptor(themeInterceptor())
                .addPathPatterns(adminPath)
                .addPathPatterns(adminPath + "/dashboard")
                .addPathPatterns(adminPath + "/menu")
                .addPathPatterns(adminPath + "/dict")
                .addPathPatterns(adminPath + "/region")
                .addPathPatterns(adminPath + "/organization")
                .addPathPatterns(adminPath + "/user")
                .addPathPatterns(adminPath + "/role")
                .addPathPatterns(adminPath + "/pdict")
                .addPathPatterns(adminPath + "/pmenu")
                .addPathPatterns(adminPath + "/pmerchant")
        		.addPathPatterns(adminPath + "/pwmenu");

        registry.addInterceptor(loginInterceptor())
                .addPathPatterns(adminPath + "/**")
                .excludePathPatterns(adminPath + "/login")
                .excludePathPatterns(adminPath + "/captcha")
                .excludePathPatterns(adminPath + "/doLogin");

    }

}

